package com.yidian.controller.system;

import com.alibaba.dubbo.config.annotation.Reference;
import com.yidian.entity.PageResult;
import com.yidian.entity.Result;
import com.yidian.pojo.system.Menu;
import com.yidian.server.system.MenuService;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * @author whb
 * @create 2021-06-08 17:56
 * @desciption 菜单控制类
 */
@RestController
@RequestMapping("/menu")
public class MenuController {
    @Reference
    private MenuService menuService;

    @GetMapping("/findAll")
    public List<Menu> findAll() {
        return menuService.findAll();
    }

    @GetMapping("/findPage")
    public PageResult<Menu> findPage(int page, int size) {
        return menuService.findPage(page, size);
    }

    @PostMapping("/findList")
    public List<Menu> findList(@RequestBody Map<String, Object> searchMap) {
        return menuService.findList(searchMap);
    }

    @PostMapping("/findPage")
    public PageResult<Menu> findPage(@RequestBody Map<String, Object> searchMap, int page, int size) {
        return menuService.findPage(searchMap, page, size);
    }

    @GetMapping("/findById")
    public Menu findById(String id) {
        return menuService.findById(id);
    }


    @PostMapping("/add")
    public Result add(@RequestBody Menu menu) {
        menuService.add(menu);
        return new Result(0, "菜单添加成功..");
    }

    @PostMapping("/update")
    public Result update(@RequestBody Menu menu) {
        menuService.update(menu);
        return new Result(0, "菜单修改成功..");
    }

    @GetMapping("/delete")
    public Result delete(String id) {
        menuService.delete(id);
        return new Result(0, "菜单删除成功..");
    }

    /**
     * 查询全部菜单生成Tree格式的Json
     * @return
     */
    @GetMapping("/findMenuToTree")
    public List<Map> findMenuToTree(){
        return menuService.findAllMenu();
    }

    /**
     * 查询全部菜单生成Tree格式的JSON（根据登录用户名）
     * @return
     */
    @GetMapping("/findMenuToTreeByLogin")
    public List<Map> findMenuByLoginNameToTree(){
        String loginName = SecurityContextHolder.getContext().getAuthentication().getName();
        return menuService.findMenuByLoginName(loginName);
    }
}
